# https://movie.pptv.com/
import os
import re
import shutil

import requests

from util.headers import get_headers_init_user_agent

root = 'static/pp视频/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)


url = 'https://movie.pptv.com/'
response = requests.get(url)
# print(response.status_code)
content = response.content.decode('utf-8')
# print(content)
# items = re.findall(r'<a class="program" onclick="window.cc.module.goToDetail("(.*?)")" _href="(.*?)" data-vid="(.*?)" channelid= "(.*?)" pitcontent="(.*?)" target="_blank"><div class="img-content" data-vid="(.*?)"><img alt="(.*?)" class="img" data-src="//img24.pplive.cn/(.*?).jpg" /><p class="info"></p></div><p class="title">(.*?)</p><p class="subTitle">(.*?)</p></a>',content)
items = re.findall(r'<img alt="(.*?)" class="img" data-src="//img24.pplive.cn/(.*?).jpg" />', content)
# print(items)
for item in items:
    print(item)
    title = item[0]
    thumb_url = f"https://img24.pplive.cn/{item[1]}.jpg"
    headers = get_headers_init_user_agent()
    headers.update({
        'referer': url
    })
    thumb_response = requests.get(thumb_url, headers=headers)
    with open(f"{root}/{title}.jpg", "wb") as f:
        f.write(thumb_response.content)